package com.dognessnetwork.operation.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dognessnetwork.operation.baseservice.DeviceTestInfoService;
import com.dognessnetwork.operation.baseservice.MessagesService;
import com.dognessnetwork.operation.configurer.MyAnnotation;
import com.dognessnetwork.operation.dognecklace.domain.DeviceTestInfo;
import com.dognessnetwork.operation.ops.domain.Messages;

@RestController
@RequestMapping(value="/devicetestinfo")
public class DeviceTestInfoJsonController {

	private @Autowired DeviceTestInfoService deviceTestInfoService;
	
	@Resource
	private	MessagesService messagesService;
	
	@PostMapping(value="/save")
	public DeviceTestInfo save(DeviceTestInfo deviceTestInfo) {
		
		DeviceTestInfo DeviceTestInfo = deviceTestInfoService.save(deviceTestInfo);
		return DeviceTestInfo;
	}
	
	@PostMapping(value="/delete_by_id")
	public void deleteById(Long id) {
		
		deviceTestInfoService.deleteById(id);
	}
	
	@PostMapping(value="/find_by_id")
	public DeviceTestInfo findById(Long id) {
		DeviceTestInfo deviceTestInfo = deviceTestInfoService.findById(id);
		return deviceTestInfo;
	}
	
	/**
     * 获取用户问题
     * @param userIdCard
     * @return
     */
	@MyAnnotation(value="获取用户问题")
	@PostMapping(value = "/get_messages_by_userIdCard")
	public String getMessagesByUserIdCard(String userIdCard) {
		// JSONArray array = new JSONArray();
		List<Object> array = new ArrayList<Object>();
		List<Object[]> messagesList = messagesService.getMessagesByUserIdCard(userIdCard);

		for (Object[] messages2 : messagesList) {
			array.add(messages2);
		}
		return array + "";
	}
	@PostMapping(value="/save_messages")
	public	Messages	saveMessages(
			@RequestParam(value="userIdCard",required=true)String	userIdCard,
			@RequestParam(value="customerIdCard",required=false)String	customerIdCard,
			@RequestParam(value="sender")boolean	sender,
			@RequestParam(value="content")String	content,
			@RequestParam(value="sendTime")String	sendTime,
			@RequestParam(value="receiveTime")String	receiveTime,
			Messages	messages){
		//log.info("\n消息：{}",messages);
		messages.setUserIdCard(userIdCard);
		messages.setCustomerIdCard(customerIdCard);
		messages.setSender(sender);
		messages.setContent(content);
		messages.setSendTime(sendTime);
		
		String	ReceiveTime=null;
		//如果true则是客服消息，不设置收到时间
		ReceiveTime = sender	? null : receiveTime ;
		messages.setReceiveTime(ReceiveTime);
		return	messagesService.save(messages);
	}
}
